package kit;

import exception.MibException;
import util.common.CharType;
import util.common.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 包含索引器和分析器适配的通用方法
 */
public class AnalyserIndexerKit {

    private static Map<String, String> doubleLetterTypeMap = null;
    private static String[] singleLetteType = {"BITS", "Counter32", "Counter64", "Gauge32", "INTEGER", "Integer32",
            "IpAddress", "TimeTicks", "Unsigned32"}; // 单单词类型
    private static String[] doubleLetterTypes = {"OBJECT", "OCTET"}; // 双单词类型
    private LetterIndexer indexer;

    static {
        doubleLetterTypeMap = new HashMap();
        doubleLetterTypeMap.put("OBJECT", "IDENTIFIER");
        doubleLetterTypeMap.put("OCTET", "STRING");
    }

    public AnalyserIndexerKit(LetterIndexer indexer) {
        this.indexer = indexer;
    }

    /**
     * 获取内置的字符索引器
     * @return
     */
    public LetterIndexer getIndexer() {
        return indexer;
    }

    /**
     * 获取Mib语法类型
     * @return
     */
    public String matchSyntaxType(){
        indexer.backupIndex();
        String type = indexer.getNextIgnoreLeftSpace(CharType.Letter, CharType.Number);
        if (type == null){
        }else if (Arrays.binarySearch(singleLetteType, type) >= 0){
            indexer.deleteIndex();
            return type;
        }else if (Arrays.binarySearch(doubleLetterTypes, type) >= 0){
            String type2 = indexer.getNextIgnoreLeftSpace(CharType.Letter);
            if (StringUtils.isEquals(type2, doubleLetterTypeMap.get(type))){
                indexer.deleteIndex();
                return StringUtils.join(" ", type, type2);
            }
        }
        indexer.recoverIndex();
        return null;
    }

    /**
     * 下一个字符是否匹配，若匹配则跳转到success状态
     * @param matcher 需要匹配的字符串
     * @param success 匹配成功跳转的状态
     * @return
     */
    public int nextMatch(String matcher, int success, int fail){
        if (indexer.isNextMatchIgnoreLeftSpace(matcher)){
            return success;
        }else{
            return fail;
        }
    }

    /**
     * str不是空白字符或null
     * @param str
     * @param success
     * @param fail
     * @return
     */
    public int notNull(String str, int success, int fail){
        if (StringUtils.isNotNullOrWhiteSpace(str)){
            return success;
        }else {
            return fail;

        }
    }

    public void ThrowAnalysisException(String errorInfo, int state, String str){
        throw new MibException(errorInfo + "state:[" + state + "], " +
                "str: ["+str+"]\n" +
                "解析位置：[" + indexer.getNext(160).trim() + "...],"
                );
    }

    /**
     * 匹配括号。
     * 传入时索引指向左括号下一位
     * 例如：(size(4))
     * 调用时，index应该要指向s
     * 返回结果：size(4))
     * @param left
     * @param right
     * @return
     */
    public String matchBrackets(char left, char right){
        int counter = 1;
        StringBuilder builder = new StringBuilder();
        char c;
        while(counter > 0){
            c = indexer.getNextChar();
            if (c == left)
                counter++;
            else if (c == right)
                counter --;
            builder.append(c);
        }
        return builder.toString();
    }

    /**
     * 匹配小括号
     * @return
     */
    public String matchBrackets(){
        return matchBrackets('(', ')');
    }
}
